//
//  TSBugReloadCollectionViewController.m
//  TSDemo_List
//
//  Created by qian on 2021/2/23.
//

#import "TSBugReloadCollectionViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CQTSNetImagesUtil.h>
#import <CQDemoKit/CQTSButtonFactory.h>
#import <CQDemoKit/CQTSContainerViewFactory.h>
#import <CJCollectionViewLayout/CQCollectionViewFlowLayout.h>
#import "TSImageReloadCollectionView.h"

@interface TSBugReloadCollectionViewController ()

@property (nonatomic, strong) TSImageReloadCollectionView *collectionView;

@end

@implementation TSBugReloadCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"测试Reload时候图片的闪烁";
    
    TSImageReloadCollectionView *collectionView = [[TSImageReloadCollectionView alloc] initWithFixColumn];
    collectionView.backgroundColor = [UIColor redColor];
    CGFloat collectionViewHeight = [self __collectionViewHeightForCollectionView:collectionView];
    
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(40);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(collectionViewHeight);
    }];
    self.collectionView = collectionView;
    
    
    UIButton *bugStateButton = [CQTSButtonFactory bugButtonWithFixBugHandle:^{
        [self.collectionView fixBug:NO];
    } reproduceBugHandle:^{
        [self.collectionView fixBug:YES];
    }];
    [self.view addSubview:bugStateButton];
    [bugStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(collectionView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(collectionView);
        make.height.mas_equalTo(44);
    }];
    
    UIView *buttonsView = [CQTSContainerViewFactory threeButtonsViewAlongAxis:MASAxisTypeVertical title1:@"reloadData(界面图片会闪烁)" actionBlock1:^(UIButton * _Nonnull bButton) {
//        [UIView performWithoutAnimation:^{
            [self.collectionView reloadData];
//        }];
        
    } title2:@"reloadSections(界面图片不会闪烁)" actionBlock2:^(UIButton * _Nonnull bButton) {
//        [UIView performWithoutAnimation:^{
            NSIndexSet *indexSex = [NSIndexSet indexSetWithIndex:0];
            [self.collectionView reloadSections:indexSex];
//        }];
        
        
    } title3:@"备用" actionBlock3:^(UIButton * _Nonnull bButton) {
        
    }];
    [self.view addSubview:buttonsView];
    [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bugStateButton.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(collectionView);
        make.height.mas_equalTo(44*3+10*2);
    }];
    
    self.collectionView.dataModels = [CQTSNetImagesUtil __getTestNetImageDataModelsWithCount:20 randomOrder:NO];
    [self.collectionView reloadData];
}



#pragma mark - Private Method
- (CGFloat)__collectionViewHeightForCollectionView:(UICollectionView *)collectionView {
    CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat collectionViewWidth = screenWidth;
    CQCollectionViewFlowLayout *flowLayout = (CQCollectionViewFlowLayout *)collectionView.collectionViewLayout;
    CGFloat collectionViewHeight = [CQCollectionViewFlowLayout heightForAllCellCount:20 maxRowCount:2 byCollectionViewWidth:collectionViewWidth withCollectionViewLayout:flowLayout];
    
    return collectionViewHeight;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
